from lib.web.rs import Resource
# from lib.util.tools import MyTools
import json

class MiscService(Resource):
    __slots__ = ()

    def __init__(self): pass
    def syncData(self, method, table, args, data):
        try:
            if table == 't_user': return self.response(self.httpStatus.INTERNAL_SERVER_ERROR, '请求为非法操作')

            if method == 'GET' or (method == 'POST' and args):
                return self.select(table, args)
            elif method == 'PUT':
                if not args and 'id' in data:
                    args = {'id': data['id']}
                if not isinstance(data, dict) or not isinstance(args, dict):
                    return self.response(self.httpStatus.BAD_REQUEST)

                data.pop('id', '')
                ok, res = self.accessor.update(table, args, data)
                if ok:
                    return self.response()
            elif method == 'DELETE':
                if not isinstance(args, dict) and 'vals' not in args:
                    return self.response(self.httpStatus.BAD_REQUEST)

                ok, res = self.accessor.delete(table, args)
                if ok: return self.response()
            else:
                if 'datas' in data and isinstance(data['datas'], str):
                    data = [json.loads(v if v[-1] == '}' else v + '}') for v in data['datas'].split('},')]
                    ok, res = self.accessor.insertMany(table, data)
                    if ok: return self.response()
                else:
                    if 'id' in data:
                        return self.response(self.httpStatus.BAD_REQUEST)

                    ok, res = self.accessor.insert(table, data)

                    if ok:
                        return self.response(data={'id': res})

            self.log.get(__name__).error(res)
            return self.response(self.httpStatus.INTERNAL_SERVER_ERROR, res)
        except Exception as e:
            self.log.get(__name__).error(e)
            return self.response(self.httpStatus.INTERNAL_SERVER_ERROR, e)
    def select(self, table, args):
        cols = args.pop('columns') if 'columns' in args else '*'
        try:
            ok, total, res = self.accessor.select(table, cols, args)
            if ok:
                return self.response(total=total, data=res)
            self.log.get(__name__).error(res)
            return self.response(self.httpStatus.INTERNAL_SERVER_ERROR, res)
        except Exception as e:
            self.log.get(__name__).error(e)
            return self.response(self.httpStatus.INTERNAL_SERVER_ERROR, e)
    def get_fp_type_list(self):
        ok, _, res = self.accessor.select('t_fp_type')
        if ok: return self.response(data=res)
        self.log.get().error('err: %s' % (res))
        return self.response(self.httpStatus.INTERNAL_SERVER_ERROR, res)
    def get_sub_type_list(self, **kwargs):
        ok, _, res = self.accessor.select('t_fp_sub_type', columns='id,name', condition={'pid': kwargs['type_id']})
        if ok: return self.response(data=res)
        self.log.get().error('err: %s' % (res))
        return self.response(self.httpStatus.INTERNAL_SERVER_ERROR, res)
    
    
    def test(self, **kwargs):
        try:
            try:
                ai_text = ""
                print(22222)
            except Exception as ex:
                self.log.get().error('[line: %s]err: %s' % (e.__traceback__.tb_lineno, ex))
            return self.response()
        except Exception as e:
            self.log.get().error(e)
            return self.response()
            

